/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buisness;

import dal.UsersDAL;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import models.Users;

/**
 * 
 * @author gabriellebotbol
 */

public class Authentication extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        resp.sendRedirect("./connection.jsp");    
    }

    

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        //String cryptPassword = Crypt.crypt(password);
        response.setContentType("text/html");

        Users user=UsersDAL.getUserByUsername(username);
        
        if(user!=null)
        {
            if(password.equals(user.getPassword()))
            {
                request.getSession().setAttribute("username", user.getUsername());
                request.getSession().setAttribute("type", "news");
                response.sendRedirect("./messages");
                
            }
            else
            {
                request.getSession().setAttribute("wrong_password", true);
                response.sendRedirect("./connection.jsp");
            }
        }
        
    }
}
